import { defineStore } from 'pinia'
import Mock from 'mockjs'

// 首先需要安装 mockjs：yarn add mockjs

// 使用 Mock.js 生成模拟数据
const mockProducts = Mock.mock({
  'products|8': [{
    'id|+1': 1,
    'title': '@ctitle(5, 10)',
    'price|100-5000.2': 1,
    'description': '@cparagraph(1)',
    'image': `https://picsum.photos/id/@integer(1,100)/400/400`
  }]
}).products

export const useProductStore = defineStore('product', {
  state: () => ({
    products: [],
    loading: false,
    error: null
  }),
  
  actions: {
    async fetchProducts() {
      this.loading = true
      try {
        // 模拟API延迟
        await new Promise(resolve => setTimeout(resolve, 500))
        this.products = mockProducts
      } catch (error) {
        this.error = error
      } finally {
        this.loading = false
      }
    },
    
    getProductById(id) {
      return this.products.find(product => product.id === parseInt(id))
    }
  }
}) 